#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<limits.h>

const int MOD_VAL = 100;

int main(int argc, char *argv[])
{
	int j, i;
	int N;
	int d; 
	char c[10];
	int *fd;
	int rand_num;

	if(argc < 3)
	{
		printf("enter 3 args:\n./a.out <d> <N>\n");
		return 0;
	}

	d = atoi(argv[1]);
	N = atoi(argv[2]);
	
	N = N > INT_MAX ? INT_MAX: N;

	fd = (int *) malloc(d*sizeof(int));

	srand( time(NULL) );

	for(i = 0; i < d; i++)
	{
		sprintf(c, "file-%d", i);
		fd[i] = open(c, O_CREAT | O_RDWR, 0777);
	}

	for(i = 0; i < d; i++)
	{
		for(j = 0; j < N; j++)
		{
			rand_num = rand() % MOD_VAL;	
			if(write(fd[i], &rand_num, sizeof(int)) == -1)
			{
				perror("write failed\n");
			}
		}
	}

	for(i = 0; i < d; i++)
	{
		close(fd[i]);
	}

	free(fd);

	return 0;
}
